package io.renren.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;

/**
 * 代码生成器
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年12月19日 下午9:12:58
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;
	
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<Map<String, Object>> list = sysGeneratorService.queryList(query);
		int total = sysGeneratorService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		//String tables = request.getParameter("tables");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		
		byte[] data = sysGeneratorService.generatorCode("user_session");
		
		response.reset();  
        response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");  
        response.addHeader("Content-Length", "" + data.length);  
        response.setContentType("application/octet-stream; charset=UTF-8");  
  
        IOUtils.write(data, response.getOutputStream());  
	}
	/**
	 * 生成代码
	 */
	@RequestMapping("/code2")
	public void code2(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		byte[] data = sysGeneratorService.generatorCode2(table);
		
		response.reset();  
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");  
		response.addHeader("Content-Length", "" + data.length);  
		response.setContentType("application/octet-stream; charset=UTF-8");  
		
		IOUtils.write(data, response.getOutputStream());  
	}
	/**
	 * 生成代码
	 */
	@RequestMapping("/test")
	public void test(HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.println(request.getParameter("app"));
	}
	private static final char[] BASE_62_CHARS = "abcdefghzjklmnopqrstuvwxyzABCDEFGHZJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
	private static final int BASE_62_CHARS_LENGTH = BASE_62_CHARS.length;
	public static String formBase10(long i){
	    StringBuffer sb = new StringBuffer();
	    if(i == 0){
	        return "a";
	    }
	    while (i > 0){
	        i = formBase10(i, sb);
	    }
	    return sb.toString();
	}
	public static long formBase10(long i, StringBuffer sb){
	    int rem = (int)(i % BASE_62_CHARS_LENGTH);
	    sb.append(BASE_62_CHARS[rem]);
	    long r = (i / BASE_62_CHARS_LENGTH);
	    return r;
	}
	public static void main(String[] args) {
		 int startIndex= 10000;
		 System.out.println(formBase10(startIndex));
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/code3")
	public void code3(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		byte[] data = sysGeneratorService.generatorCode3(table);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}



	/**
	 * 生成代码
	 */
	@RequestMapping("/domain")
	public void codeDomain(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		String template="template/BeanEntity.xml.vm";
		byte[] data = sysGeneratorService.generatorCode4(table,template);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/view")
	public void codeView(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		String template="template/Bean.xml.vm";
		byte[] data = sysGeneratorService.generatorView(table,template);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/menu")
	public void codeMenu(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		String template="template/Menu.xml.vm";
		byte[] data = sysGeneratorService.generatorMenu(table,template);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/messages")
	public void codeMessages(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		String template="template/MessagesZh.csv.vm";
		byte[] data = sysGeneratorService.generatorMessages(table,template);

		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=\"renren.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/outTest")
	public void codeOutTest(HttpServletRequest request, HttpServletResponse response) throws IOException{
		//String[] tableNames = new String[]{};
		String table = request.getParameter("table");
		//tableNames = JSON.parseArray(tables).toArray(tableNames);
		String template="template/MessagesZh.csv.vm";
		sysGeneratorService.codeOutTest(table,template);
		response.getWriter().print("已生成");
	}
}
